﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ParangonBiz.Data;
using ParangonBiz.SiteMapDataProvider;
using ParangonBiz.Tools;

namespace ParangonBiz
{
    public partial class Info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            

            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    string sId = Request.QueryString["Id"];
                    int iId;
                    if (int.TryParse(sId, out iId))
                    {
                        List<SiteMenuItem> sm = SiteMenuItem.GetMenuItemsByKeyId(iId, (int)EnumHelper.PageType.InformationBlock);
                        if (sm != null)
                        {
                            rptSubMenu.DataSource = sm.OrderBy(i => i.Title);
                            rptSubMenu.DataBind();
                        }

                        SiteContent targetSiteContent = SiteContent.Get(iId);
                        if (targetSiteContent != null)
                        {
                            lMainContent.Text = targetSiteContent.Content;
                            lMainContentTitle.Text = targetSiteContent.Title;

                            rptFiles.DataSource = targetSiteContent.FileAttachements;
                            rptFiles.DataBind();

                            List<KeyValuePair<string, Uri>> nodes = new List<KeyValuePair<string, Uri>>();

                            SiteMenuItem smi = SiteMenuItem.GetMenuItemParent(iId, (int)EnumHelper.PageType.InformationBlock);

                            if (smi != null)
                            {
                                nodes.Add(new KeyValuePair<string, Uri>(smi.Title,
                                    new Uri(Request.Url,
                                        String.Format("/Info.aspx?id={0}", smi.KeyId))));
                            }

                            nodes.Add(new KeyValuePair<string, Uri>(targetSiteContent.Title, Request.Url));
                            ((AxSiteMapDataProvider)SiteMap.Provider).Stack(nodes);
                        }
                    }
                }
            }
        }
    }
}